<template>
  <div class="register-all">
    <div class="register">
      <div class="re-title">Two Shop注册页面</div>
      <el-form ref="form" status-icon :rules="rules" :model="form" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="roleName">
          <el-input v-model="form.roleName"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" type="email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="tel">
          <el-input v-model="form.tel"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio :label="0">男</el-radio>
            <el-radio :label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生日">
          <el-date-picker
            v-model="form.birthday"
            align="right"
            type="date"
            placeholder="选择日期"
            :picker-options="pickerOptions1"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="真实姓名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 320px" round type="primary" @click="onSubmit('form')">立即注册</el-button>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 320px" @click="toLogin" round>已注册？点此登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    let checkUsername = (rule, value, callback) => {
      this.axios.get("/user/checkUsername?username=" + value).then(res => {
        if (res.data.status !== 200) {
          callback(new Error(res.data.msg));
        } else {
          callback();
        }
      });
    };
    return {
      form: {
        username: "",
        password: "",
        roleName: "",
        sex: "",
        birthday: "",
        email: "",
        tel: "",
        name: ""
      },
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            }
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            }
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            }
          }
        ]
      },
      rules: {
        username: [
          { required: true, message: "用户名是必须的", trigger: "change" },
          { validator: checkUsername, trigger: ["blur", "change"] }
        ],
        password: [{ required: true, message: "密码是必须的" }],
        roleName: [{ required: true, message: "昵称是必须的" }],
        sex: [{ required: true, message: "性别是必须的" }],
        email: [
          { required: true, message: "电子邮箱是必须的" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        tel: [
          { required: true, message: "电话是必须的", trigger: "change" },
          {
            len: 11,
            message: "请输入正确的电话号码",
            trigger: ["blur", "change"]
          }
        ]
      }
    };
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$myPost(this, "/user/register", this.toLogin, this.form);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    toLogin: function() {
      this.$router.push({ path: "/login" });
    }
  }
};
</script>

<style scoped lang="scss">
.register-all {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  min-height: 600px;
}

.register {
  width: 600px;
  padding: 20px 100px;
  border-radius: 20px;
  border: 1px solid #999;
  > .re-title {
    text-align: center;
    color: #409eff;
    font-size: 30px;
    padding-bottom: 20px;
    font-weight: bold;
  }
}
</style>